//-----------------------------------------------------------------------
// <copyright file="a_Products_OptionDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_Products_OptionDAOBase
    {
        #region Common methods
        public virtual a_Products_Option Createa_Products_OptionFromReader(IDataReader reader)
        {
            a_Products_Option item = new a_Products_Option();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("OptionID"))) item.OptionID = (int)reader["OptionID"];
                if (!reader.IsDBNull(reader.GetOrdinal("OptionName"))) item.OptionName = (string)reader["OptionName"];
                if (!reader.IsDBNull(reader.GetOrdinal("OptionLabel"))) item.OptionLabel = (string)reader["OptionLabel"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
                if (!reader.IsDBNull(reader.GetOrdinal("StoreID"))) item.StoreID = (int)reader["StoreID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ItemCount"))) item.ItemCount = (int)reader["ItemCount"];
                if (!reader.IsDBNull(reader.GetOrdinal("TempOptionID"))) item.TempOptionID = (int)reader["TempOptionID"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Products_Option methods
            
        public virtual void Createa_Products_Option(a_Products_Option a_Products_Option)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Products_Options_ins");
                
                database.AddInParameter(dbCommand, "@OptionName", DbType.AnsiString, a_Products_Option.OptionName);
                database.AddInParameter(dbCommand, "@OptionLabel", DbType.AnsiString, a_Products_Option.OptionLabel);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Products_Option.DomainID);
                database.AddInParameter(dbCommand, "@StoreID", DbType.Int32, a_Products_Option.StoreID);
                database.AddInParameter(dbCommand, "@ItemCount", DbType.Int32, a_Products_Option.ItemCount);
                database.AddInParameter(dbCommand, "@TempOptionID", DbType.Int32, a_Products_Option.TempOptionID);
                database.AddOutParameter(dbCommand, "@OptionID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Products_Option.OptionID = (int)database.GetParameterValue(dbCommand, "@OptionID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Products_Option methods
        
        public virtual void Updatea_Products_Option(a_Products_Option a_Products_Option)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Products_Options_upd");            
                
                database.AddInParameter(dbCommand, "@OptionID", DbType.Int32, a_Products_Option.OptionID);
                database.AddInParameter(dbCommand, "@OptionName", DbType.AnsiString, a_Products_Option.OptionName);
                database.AddInParameter(dbCommand, "@OptionLabel", DbType.AnsiString, a_Products_Option.OptionLabel);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Products_Option.DomainID);
                database.AddInParameter(dbCommand, "@StoreID", DbType.Int32, a_Products_Option.StoreID);
                database.AddInParameter(dbCommand, "@ItemCount", DbType.Int32, a_Products_Option.ItemCount);
                database.AddInParameter(dbCommand, "@TempOptionID", DbType.Int32, a_Products_Option.TempOptionID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Products_Option methods
        public virtual void Deletea_Products_Option(int optionID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Products_Options_del");
                
                database.AddInParameter(dbCommand, "@OptionID", DbType.Int32, optionID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Products_Option methods
        
        public virtual a_Products_Option Geta_Products_Option(int optionID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Products_Options_sel");
                
                database.AddInParameter(dbCommand, "@OptionID", DbType.Int32, optionID);
                
                a_Products_Option a_Products_Option =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Products_Option = Createa_Products_OptionFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Products_Option;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_Products_OptionList methods
        public virtual a_Products_OptionCollection Geta_Products_OptionList(a_Products_OptionColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Products_Options_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_Products_OptionCollection a_Products_OptionCollection = new a_Products_OptionCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Products_Option a_Products_Option = Createa_Products_OptionFromReader(reader);
                        a_Products_OptionCollection.Add(a_Products_Option);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_Products_OptionCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_Products_OptionCollection Geta_Products_OptionList(a_Products_OptionColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_Products_OptionList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}